#include "head.h"

//初始化线程池
void init_thread_pool(thread_pool_t *pool, int num){

    //线程ID
    pool->threadIDs = (pthread_t *)malloc(num * sizeof(pthread_t));
    ERROR_CHECK(pool->threadIDs, NULL, "malloc_initPool.c");
    //线程个数
    pool->num = num;
    //互斥锁
    int ret = pthread_mutex_init(&(pool->lock), NULL);
    ERROR_CHECK(ret, -1, "pthread_mutex_init_initPool.c");
    //条件变量
    ret = pthread_cond_init(&(pool->cond), NULL);
    ERROR_CHECK(ret, -1, "pthread_cond_init_initPool.c");
    //任务队列
    bzero(&(pool->queue), sizeof(pool->queue));

    //创造线程池
    for(int i = 0; i < num; i++){
        ret = pthread_create(&(pool->threadIDs[i]), NULL, thread_main, pool);
        ERROR_CHECK(ret, -1, "pthread_create_initPool.c");
    }
}
